


//
//  ShangMenViewController.m
//  GoShop
//
//  Created by mac22 on 14/11/17.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "ShangMenViewController.h"
#import "Grouble.h"
#import "UITableView+JFTableView.h"
#import "MJRefresh.h"
#import "ServiceAdModel.h"
#import "SelectFindHttp.h"
#import "GetUrlString.h"
#import "ImageScrollTableViewCell.h"
#import "ShangHomeTableViewCell.h"
#import "HomeServiceModel.h"
#import "WebServerViewController.h"
#import "GetUrlString.h"

@interface ShangMenViewController ()<UITableViewDelegate,UITableViewDataSource,MJRefreshBaseViewDelegate,ShangHomeTableViewCellDelegate>
{

        NSMutableArray *_advArray;//广告数据源
        NSMutableArray *_homeServiceArray;//上门服务数据源
        NSMutableArray *_advImageUrlArray;//广告图片数组

}
@property (nonatomic,strong)UITableView * myTableView;
@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;

@end

@implementation ShangMenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上门";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initData];
    [self setTableViews];
    
    [self setRush];
    // Do any additional setup after loading the view.
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.myTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.myTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}

- (void)loadMoreData
{
    ZCJLog(@"loadMoreData");
}
- (void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 加载广告数据
        [self loadAdData];
        [self loadHomeService];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            ZCJLog(@"%@",[NSThread currentThread]);
        });
    });
    
    ZCJLog(@"loadNewData");
}
// 加载home数据
- (void)loadHomeService
{
    NSString * homeService = [[GetUrlString sharedManager]urlWithVisitService];
    [SelectFindHttp getWirhUrl:homeService parms:nil success:^(id json) {
        [_homeServiceArray removeAllObjects];
        NSMutableArray *dataArray = [json objectForKey:@"data"];
        for (int i = 0; i < dataArray.count; ++i) {
            HomeServiceModel *homeM = [HomeServiceModel objectWithKeyValues:dataArray[i]];
            [_homeServiceArray addObject:homeM];
            [_homeServiceArray addObject:homeM];
        }
        [self.myTableView reloadData];
    } failture:^(id error) {
        
    }];
}
- (void)loadAdData
{
    NSString * urlAd  =[[GetUrlString sharedManager]urlWithVisitAd];
   // ZCJLog(@"<#string#>")
    [_advImageUrlArray removeAllObjects];
    [_advArray removeAllObjects];
    [SelectFindHttp getWirhUrl:urlAd parms:nil success:^(id json) {
       
       // ZCJLog(@"%@",json);
        
        NSMutableArray *dataArray = [json objectForKey:@"data"];
        for (int i = 0; i < dataArray.count;  ++i) {
            ServiceAdModel *serviceAdvM = [ServiceAdModel objectWithKeyValues:dataArray[i]];
            [_advArray addObject:serviceAdvM];
            // 放两个是可以看到效果
            [_advImageUrlArray addObject:serviceAdvM.imgUrl];
            [_advImageUrlArray addObject:serviceAdvM.imgUrl];
            
        }
        
        [self.head endRefreshing];
        //ZCJLog(@"%@",_advImageUrlArray);
        [self.myTableView reloadData];

    } failture:^(id error) {
        
    }];
    if (_advImageUrlArray.count == 0) {
        [_advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
        [_advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
    }
}
- (void)setTableViews
{ 
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
}
- (void)initData
{
    _advArray = [NSMutableArray array];
    _advImageUrlArray = [NSMutableArray array];
    _homeServiceArray = [NSMutableArray array];
}
#pragma mark - tablView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ImageScrollTableViewCell * cell = [ImageScrollTableViewCell ImageScrollTableViewCell:tableView frame:CGRectMake(0, 0, ScreenWidth, 155)];
        
        if (_advImageUrlArray.count !=0) {
            ZCJLog(@"%@",_advImageUrlArray);
            [cell setImageArray:_advImageUrlArray];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else if (indexPath.row == 1){
        ShangHomeTableViewCell * cell = [ShangHomeTableViewCell ShangHomeTableViewCell:tableView];
        [cell setHomeServiceArray:_homeServiceArray];
        return cell;
    }else{
        return nil;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 155;
    }else if(indexPath.row == 1){
        return 300;
    }
    return 800;
}
- (void)ShangHomeTableViewCellClick:(NSIndexPath *)indexPath
{
    HomeServiceModel * serviceModel = _homeServiceArray[0];
    
    NSString *urlStr =  [[GetUrlString sharedManager]urlWithHomeServerWebData:serviceModel];
    ZCJLog(@"%@",urlStr);
    
    WebServerViewController * webSerView = [[WebServerViewController alloc]init];
    webSerView.urlStr = urlStr;
    [self.navigationController pushViewController:webSerView animated:YES];
    
    ZCJLog(@"%@",indexPath);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCJLog(@"indkjflfldlkflf");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
